﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace SampleExamLinqXml
{
    class Program
    {
        static void Main(string[] args)
        {
            //int firstTeamId=AddTeam("Tothnum", "Airen", "London", "Top", "50000");
            //int secondTeamId=AddTeam("Everton","Ev" ,"Manchester City", "Anfield", "65000");
            //AddMatch(DateTime.Now,firstTeamId,secondTeamId,1,1);
            CreateXml();    
        }
        public static void CreateXml()
        {
            string fileName = "statistics.xml";
            Encoding encoding = Encoding.GetEncoding("windows-1251");
            using (XmlTextWriter writer = new XmlTextWriter(fileName, encoding))
            {
                writer.Formatting = Formatting.Indented;
                writer.IndentChar = '\t';
                writer.Indentation = 1;
                writer.WriteStartDocument();
                writer.WriteStartElement("teams");
                FootballDataContext context = new FootballDataContext(); Console.WriteLine();
                var stats = context.FullStatistics(new DateTime(2009, 1, 1), new DateTime(2010, 12, 31));
                foreach (var item in stats)
                {
                    writer.WriteStartElement("team");
                    writer.WriteAttributeString("name", item.teamName);
                    writer.WriteStartElement("score");
                    writer.WriteString((item.wins*3+item.draws).ToString());
                    writer.WriteEndElement();
                    string[] activesPassivs = item.scores.Split(':');
                    writer.WriteStartElement("goal-active");
                    writer.WriteString(activesPassivs[0]);
                    writer.WriteEndElement();
                    writer.WriteStartElement("goal-passive");
                    writer.WriteString(activesPassivs[1]);
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }

        }



        public static void PrintScore()
        {
            FootballDataContext context = new FootballDataContext(); Console.WriteLine();
            var stats = context.FullStatistics(new DateTime(2009, 1, 1), new DateTime(2010, 12, 31));
            List<Statistics> myStatisticList = new List<Statistics>();
            foreach (var item in stats)
            {
                myStatisticList.Add(new Statistics(item.teamName, item.wins, item.loses, item.draws, item.scores, item.wins * 3 + item.draws));
            }
            myStatisticList.Sort();
            myStatisticList.Reverse();
            foreach (var item in myStatisticList)
            {
                Console.WriteLine(item.Name + " " + item.Wins + " " + item.Draws + " " + item.Loses + " " + item.Scores + " " + item.Points);
            }
        }

        static int AddTeam(string name, string owner, string city, string stadiumName, string stadiumCapacity)
        {
            FootballDataContext context = new FootballDataContext();
            // Create new order object
            Team team = new Team()
            {
                Name = name,
                Owner = owner,
                City = city,
                StadiumName = stadiumName,
                StadiumCapacity = stadiumCapacity,
            };
            // Mark the object for inserting
            context.Teams.InsertOnSubmit(team);
            context.SubmitChanges();
            return team.TeamId;
        }
        static int AddMatch(DateTime matchDate, int homeId, int awayId, int homeTeamScore, int awayTeamScore)
        {
            FootballDataContext context = new FootballDataContext();
            // Create new order object
            Match match = new Match()
            {
        
                MatchDate=matchDate,
                HomeId=homeId,
                AwayId=awayId,
                HomeTeamScore=homeTeamScore,
                AwayTeamScore=awayTeamScore,
            };
            // Mark the object for inserting
            context.Matches.InsertOnSubmit(match);
            context.SubmitChanges();
            return match.MatchId;
        }
    }
}
